package com.wonders.frame.autoGeneration.task;

import com.wonders.frame.autoGeneration.support.TaskContent;
import com.wonders.frame.autoGeneration.task.Core.Task;
import com.wonders.frame.common.util.FileUtil;
import com.wonders.frame.common.util.HumpUtil;

import java.io.File;
import java.util.concurrent.Callable;

/**
 * @author 顾乘瑞
 */
public class GenControllerTask extends Task {


    private static final String PATH = System.getProperty("file.separator") +"src"
            + System.getProperty("file.separator") + "main"
            + System.getProperty("file.separator") + "java"
            + System.getProperty("file.separator") + "com"
            + System.getProperty("file.separator") + "wonders"
            + System.getProperty("file.separator") + "frame"
            + System.getProperty("file.separator") + "controller"
            + System.getProperty("file.separator");

    public GenControllerTask(final TaskContent cont){
        super(cont);
    }

    @Override
    public Object call(){
        String tableName = cont.getTableName();
        String entityParamName = HumpUtil.lineToHump(tableName);
        String entityClassName = HumpUtil.tableName2ClassName(tableName);
        String className = entityClassName + "Controller";

        String filePath = TaskContent.PROJECT_PATH + PATH;
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        String writeFilePath = filePath + className + ".java";
        StringBuilder content = new StringBuilder("package com.wonders.frame.controller;\n" +
                "\n" +
                "import com.gcr.server.response.Response;\n" +
                "import com.wonders.frame.common.Enum.SessionKeyEnum;\n" +
                "import com.wonders.frame.controller.core.CoreController;\n" +
                "import com.wonders.frame.entity.AuthAccount;\n" +
                "import com.wonders.frame.entity." + entityClassName + ";\n" +
                "import com.wonders.frame.service." + entityClassName + "Service;\n" +
                "import com.wonders.frame.service.Core.CoreCurdService;\n" +
                "import org.slf4j.Logger;\n" +
                "import org.slf4j.LoggerFactory;\n" +
                "import org.springframework.beans.factory.annotation.Autowired;\n" +
                "import org.springframework.web.bind.annotation.RequestBody;\n" +
                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                "import org.springframework.web.bind.annotation.RestController;\n" +
                "\n" +
                "import javax.servlet.http.HttpServletRequest;\n" +
                "import javax.servlet.http.HttpSession;\n" +
                "\n" +
                "\n" +
                "@RestController\n" +
                "@RequestMapping(\"/" + entityParamName + "\")\n" +
                "public class " + entityClassName + "Controller extends CoreController<" + entityClassName + "> {\n" +
                "    Logger log = LoggerFactory.getLogger(" + entityClassName + "Controller.class);\n" +
                "\n" +
                "    @Autowired\n" +
                "    private " + entityClassName + "Service " + entityParamName + "Service;\n" +
                "    @Override\n" +
                "    protected CoreCurdService getService() {\n" +
                "        return " + entityParamName + "Service;\n" +
                "    }\n" +
                "\n" +
                "    @RequestMapping(\"save\")\n" +
                "    public Response save(@RequestBody " + entityClassName + " news, HttpServletRequest request){\n" +
                "        try {\n" +
                "            log.info(\"request:" + entityParamName + "/save\");\n" +
                "            log.info(news.toString());\n" +
                "            HttpSession session = request.getSession();\n" +
                "            AuthAccount acc =  (AuthAccount)session.getAttribute(SessionKeyEnum.USERINFO.name());\n" +
                "            " + entityParamName + "Service.save(news, acc == null ? \"\" : acc.getUser().getUserName());\n" +
                "            return successResponse(news);\n" +
                "\n" +
                "        }catch (Exception e){\n" +
                "            e.printStackTrace();\n" +
                "            return errorResponse();\n" +
                "        }\n" +
                "    }\n" +
                "}\n");
        FileUtil.createFile(content.toString(), writeFilePath);
        return null;
    }
}
